var request = require("request");
var fs = require('fs');                //获取fs模块



var COOKIE = '_xsrf=AeNIB6zOv68AODZZy03KgQhbtS2IUuYv; _zap=165259da-b022-4056-a631-159e037062a5; d_c0="AKBujhZMWhCPTnImJT1zIYcNa9tGztUuGQg=|1573717650"; _ga=GA1.2.404256878.1582787388; z_c0="2|1:0|10:1593311246|4:z_c0|92:Mi4xSlFacUFnQUFBQUFBb0c2T0ZreGFFQ1lBQUFCZ0FsVk5EazdsWHdDYUtNX21uTWpHdUJrQXVFdUZzQko2UWoyTGl3|6edc51b3785d7f231be0a451179971ef8b7597488e881f595ee41740b5c501c9"; tst=h; tshl=; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1598943343,1598950289,1600063709,1600090197; q_c1=bfed34552b974ab6ab44ce50665dd06f|1600090291000|1574048542000; _gid=GA1.2.866741704.1600224702; SESSIONID=mGbBs8Mzi3eDyUOcpa2M498ot63JYHkBY7IYOdhyeOV; JOID=VlETCk5kbK4CVYpiB2kgOnM07eIUBCTDTCe_IHIMUsRJIN4ga94RzFBZiGIEQ2zT2hU2Cyi2TbFuTXt86nizpQw=; osd=Ul0XBUlgYKoNUo5uA2YnPn8w4uUQCCDMSyOzJH0LVshNL9kkZ9oey1RVjG0DR2DX1RIyByy5SrViSXR77nS3qgs=; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1600225806; KLBRSID=57358d62405ef24305120316801fd92a|1600225835|1600224686';

var access_token = '3fc5ceb52ffc5e6c1d9f6b25df851013f9124faa0a1821e71c376d3da7884bc5';

var TEMP_ZAN = '';
var TEMP_BOX = '';

function getLocalData(){
    var localData = fs.readFileSync('zhihu-log.js','utf-8');
    try {
        localData = JSON.parse(localData);
        TEMP_ZAN = localData.TEMP_ZAN;
        TEMP_BOX = localData.TEMP_BOX;
    } catch (error) {
        console.log('本地数据不是json')
    }
}
function writeLocalData(){
    var data = {
        'TEMP_ZAN':TEMP_ZAN,
        'TEMP_BOX':TEMP_BOX,
    };
    fs.writeFileSync('zhihu-log.js', JSON.stringify(data));
}
function sendData(content,isAtAll){
    var options = { 
        method: 'POST',
        url: 'https://oapi.dingtalk.com/robot/send',
        qs: { 
            access_token: access_token 
        },
        headers: 
        { 'Postman-Token': '207df61c-e31b-4d4f-b7b3-aca8931347e5',
            'cache-control': 'no-cache',
            'Content-Type': 'application/json' },
        body: {
            "msgtype": "text", 
            "text": {
                "content": content
            },
            "at": {
                "isAtAll": !!isAtAll
            }
        },
        json: true
    };

    request(options, function (error, response, body) {
        if (error) throw new Error(error);
        console.log(body);
    });

}

function getDataThank() {
    var options = { 
        method: 'GET',
        json: true,
        encoding: 'utf-8',
        url: 'https://www.zhihu.com/api/v4/notifications/v2/vote_thank',
        headers: 
        {  
            cookie: COOKIE,
         } 
    };
    request(options, function (error, response, body) {
        if (error) throw new Error(error);
        
        if(response.statusCode===200){
            //console.log(body);
            var data = body.data;
            var contentArr =[];
            data.forEach(function(i){
                contentArr.push(''+i.content.actors[0].name+'    -    '+i.content.verb);
            });
            var contentStr = contentArr.join('\n');
            if(TEMP_ZAN != contentStr){
                TEMP_ZAN = contentStr;
                writeLocalData();
                sendData(new Date().toLocaleString()+'    消息如下:\n'+contentStr);
            }
        }else{
            sendData('消息如下：\n'+JSON.stringify(error));
            clearInterval(timer);
        }
    
    });

}

function getDataBox() {
    var options = { 
        method: 'GET',
        json: true,
        encoding: 'utf-8',
        url: 'https://www.zhihu.com/api/v4/inbox?limit=20',
        headers: 
        {  
            cookie: COOKIE,
         } 
    };
    request(options, function (error, response, body) {
        if (error) throw new Error(error);
        
        if(response.statusCode===200){
            //console.log(body);
            var data = body.data;
            var contentArr =[];
            if(data.length>0){
                contentArr.push(data[0].snippet);
            }
            var contentStr = contentArr.join('\n');
            if(TEMP_BOX != contentStr){
                TEMP_BOX = contentStr;
                writeLocalData();
                sendData(new Date().toLocaleString()+'    消息如下:\n'+contentStr);
            }
        }else{
            sendData('消息如下：\n'+JSON.stringify(error));
            clearInterval(timer);
        }
    
    });

}
getLocalData();
getDataThank();
getDataBox(); 

//每15分钟提醒一次
var timer = setInterval(() => {
    getDataThank();
    getDataBox(); 
}, 8*60*1000);


